package tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class PhoneDao {
  @Autowired
  private JdbcTemplate jdbcTemplate;

  /**
   * 根据手机的id查询价格
   *
   * @param id
   * @return
   */
  public int findPriceById(String id) {
    String sql = "SELECT price FROM phone WHERE id=?";
    Integer price = jdbcTemplate.queryForObject(sql, Integer.class, id);
    return price;
  }

  /**
   * 更新用户账户余额
   *
   * @param userName
   * @param price
   */
  public void updateBalance(String userName, int price) {
    String sql = "UPDATE account SET balance=balance-? WHERE username=?";
    jdbcTemplate.update(sql, price, userName);
  }

  /**
   * 更新手机库存
   *
   * @param id
   */
  public void updateStock(String id) {
    String sql = "UPDATE phone_stock1 SET stock=stock-1 WHERE id=?";
    jdbcTemplate.update(sql, id);
  }

  /**
   * 更新手机价格
   * @param id
   * @param amount
   */
  public void updatePrice(String id, int amount) {
    String sql = "UPDATE phone SET price=? WHERE id=?";
    jdbcTemplate.update(sql, amount, id);
  }
}
